package com.bishe.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.entity.Association;
import com.bishe.entity.PersonnelInfo;
import com.bishe.entity.query.PersonnelInfoQuery;
import com.bishe.mapper.PersonnelInfoMapper;
import com.bishe.service.AssociationService;
import com.bishe.service.PersonnelInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author wwz
* @description 针对表【personnel_info】的数据库操作Service实现
* @createDate 2024-03-04 13:57:33
*/
@Service
public class PersonnelInfoServiceImpl extends ServiceImpl<PersonnelInfoMapper, PersonnelInfo>
    implements PersonnelInfoService {

    @Resource
    private AssociationService associationService;

    @Override
    public IPage<PersonnelInfo> getPage(PersonnelInfoQuery query) {

        IPage<PersonnelInfo> page = page(query.toPage(), Wrappers.lambdaQuery(PersonnelInfo.class)
                .like(StrUtil.isNotBlank(query.getName()), PersonnelInfo::getName, query.getName())
                .like(StrUtil.isNotBlank(query.getIdCard()), PersonnelInfo::getIdCard, query.getIdCard())
                .like(StrUtil.isNotBlank(query.getPhone()), PersonnelInfo::getPhone, query.getPhone())
                .like(StrUtil.isNotBlank(query.getEdBackground()), PersonnelInfo::getEdBackground, query.getEdBackground())

        );

        List<PersonnelInfo> records = page.getRecords();
        if (CollUtil.isEmpty(records)){
            return page;
        }

        for (PersonnelInfo record : records) {
            List<Association> one = associationService.list(Wrappers.lambdaQuery(Association.class)
                    .eq(Association::getPid, record.getId()));

            if (CollUtil.isNotEmpty(one)){
                record.setPersonnelType(one.get(0).getType());
            }
        }

        Boolean type = query.getType();

        IPage<PersonnelInfo> resultPage = new Page<>();

        if (type != null){
            List<PersonnelInfo> resultList = records.stream().filter(s -> type.equals(s.getPersonnelType())).collect(Collectors.toList());
            resultPage.setRecords(resultList);
        }else {
            resultPage.setRecords(records);
        }
        resultPage.setPages(page.getSize());
        resultPage.setCurrent(page.getCurrent());
        resultPage.setTotal(page.getTotal());
        return resultPage ;
    }
}




